/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.resource.service.impl;

import io.jsonwebtoken.lang.Collections;
import io.swagger.models.auth.In;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.mach.entity.DrawingAttachApproval;
import org.springblade.mach.service.IDrawingAttachApprovalService;
import org.springblade.modules.resource.entity.Attach;
import org.springblade.modules.resource.vo.AttachVO;
import org.springblade.modules.resource.mapper.AttachMapper;
import org.springblade.modules.resource.service.IAttachService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 附件表 服务实现类
 *
 * @author Chill
 */
@Service
public class AttachServiceImpl extends BaseServiceImpl<AttachMapper, Attach> implements IAttachService {
	//懒加载，否则会循环引用错误
	@Lazy
	@Autowired
	private IDrawingAttachApprovalService drawingAttachApprovalService;

	@Override
	public List<Attach> getSingleAttachByInfoId(List<Long> infoIdList) {
		return baseMapper.getSingleAttachByInfoId(infoIdList);
	}

	@Override
	public List<Attach> getAttachByInfoId(Long infoId) {
		return baseMapper.getAttachByInfoId(infoId);
	}

	@Override
	public List<AttachVO> getImgListByUploadId(Long uploadId) {
		return baseMapper.getImgListByUploadId(uploadId);
	}

	@Override
	public IPage<AttachVO> selectAttachPage(IPage<AttachVO> page, AttachVO attach) {
		return page.setRecords(baseMapper.selectAttachPage(page, attach));
	}

	@Override
	public List<AttachVO> getAllAttachListById(Long uploadId) {
		return baseMapper.getAllAttachListById(uploadId);
	}

	@Override
	public List<AttachVO> getDrawingByUploadId(Long uploadId) {
		return baseMapper.getDrawingByUploadId(uploadId);
	}

	@Override
	public List<AttachVO> getThreeDrawingByUploadId(Long uploadId) {
		return baseMapper.getThreeDrawingByUploadId(uploadId);
	}

	@Override
	public List<AttachVO> getDeleteAttach(Long uploadId, List<String> fileName) {
		return baseMapper.getDeleteAttach(uploadId,fileName);
	}

	@Override
	public List<AttachVO> getDeleteThreeAttach(Long uploadId, List<String> threeFileName) {
		return baseMapper.getDeleteThreeAttach(uploadId, threeFileName);
	}

	@Override
	public List<AttachVO> getBomExcelByUploadId(Long uploadId) {
		return baseMapper.getBomExcelByUploadId(uploadId);
	}

	@Override
	public AttachVO getAttachByUploadIdAndDrawingNumber(Long uploadId, String drawingNumber) {
		return baseMapper.getAttachByUploadIdAndDrawingNumber(uploadId, drawingNumber);
	}

	@Override
	public List<AttachVO> getChildAttachList(Long uploadId, String drawingNumber) {
		return baseMapper.getChildAttachList(uploadId, drawingNumber);
	}



	@Override
	public List<AttachVO> findAllUploadId(Long uploadId) {
		return baseMapper.findAllUploadId(uploadId);
	}

	@Override
	public List<AttachVO> findDrawingListByUploadId(Long uploadId, String taskDefinitionKey) {
		List<AttachVO> attachList = baseMapper.findDrawingListByUploadId(uploadId);
		attachList.stream().forEach(attachVO -> {
			DrawingAttachApproval drawingAttachApproval = new DrawingAttachApproval();
			drawingAttachApproval.setUploadId(uploadId);
			drawingAttachApproval.setAttachId(attachVO.getId());
			drawingAttachApproval.setTaskDefinitionKey(taskDefinitionKey);
			DrawingAttachApproval approval = drawingAttachApprovalService.getDrawingAttachApproval(drawingAttachApproval);
			if (approval != null) {
				attachVO.setApprovalResult(approval.getApprovalResult());
				attachVO.setApprovalOpinion(approval.getApprovalOpinion());
			}
		});
		return attachList;
	}


	@Override
	@Transactional
	public Boolean deleteAttachByIds(List<Long> attachIdList) {
		if (Collections.isEmpty(attachIdList)) {
			throw new ServiceException("参数不能为空!");
		}
		baseMapper.deleteAttachByIds(attachIdList);
		return true;
	}

	@Override
	@Transactional
	public Boolean uploadAttachStatus(List<Long> attachIdList, Integer statusCode) {
		Boolean result = false;
		if (Collections.isEmpty(attachIdList)) {
			throw new ServiceException("附件主键列表不能为空!");
		}
		if (statusCode == null) {
			throw new ServiceException("附件状态不能为空!");
		}
		Integer resultNum = 0;
		resultNum = baseMapper.uploadAttachStatus(attachIdList, statusCode);
		if (resultNum.equals(attachIdList.size())){
			result = true;
		}
		return result;
	}

	@Override
	public Attach getAttachByDrawingInfoId(Long drawingInfoId) {
		if (drawingInfoId == null) {
			throw new ServiceException("图纸主键不能为空!");
		}
		return baseMapper.getAttachByDrawingInfoId(drawingInfoId);
	}

}
